#!/bin/bash

########################################################################
# This file is released under the Creative Commons Attribution-Share 
# Alike 3.0 License
#
# All enclosed files are copyright (C) 2007 Mike Jones & FlashGen.Com, 
# unless otherwise indicated.
#
# You can redistribute it and/or modify it under the terms of the 
# CREATIVE COMMONS ATTRIBUTION-SHARE ALIKE 3.0 License as published by 
# the Creative Commons.
#
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
#
# See the Creative Commons Attribution-Share Alike 3.0 License for 
# more details.
#
# For any queries not directly related to the CREATIVE COMMONS 
# ATTRIBUTION-SHARE ALIKE 3.0 LICENSE please use the following methods 
# of contact:
#
# web: http://www.flashgen.com
# email: fgdevelopment@flashgen.com
#
########################################################################

########################################################################
					# Set up our variables #
########################################################################

# These are just simple string stores to use for the opening and closing
# of our config file.

xmlHeader="<?xml version=\"1.0\"?>"
configHeader="<flex-config>"
configFooter="</flex-config>"
info="<!--
	This file was auto-generated using FlashGen.Com's Flex Ant scripts for Flex Builder
	Created by Mike Jones (fgdevelopment@flashgen.com) and released under the Creative 
	Commons Attribution-Share Alike 3.0 License
-->"

# These are used to form the compiler section of the Flex-config XML file.
compilerSectionHeader="\t<compiler>\n\t\t<!-- Turn on generatation of accessible swfs. -->"
compilerSectionFooter="\t</compiler>"     
accessible="\t\t<accessible>$3</accessible>"
asWarnings="\t\t<show-actionscript-warnings>$4</show-actionscript-warnings>"
debug="\t\t<debug>$5</debug>"
keepAS="\t\t<keep-generated-actionscript>$6</keep-generated-actionscript>"
namespaceHeader="\t\t<namespaces>\n\t\t\t<!-- Specify a URI to associate with a manifest of components for use as MXML -->\n\t\t\t<namespace>"
namespaceFooter="\t\t\t</namespace>\n\t\t</namespaces>"
namespaceURI="\t\t\t\t<uri>$7</uri>"
manifestFile="\t\t\t\t<manifest>$8</manifest>"
optimized="\t\t<!-- Enable post-link SWF optimization. -->\n\t\t<optimize>$9</optimize>"

# These values are used to create the META section of the Flex-config XML file.
metaSectionHeader="\t<!-- Metadata added to SWFs via the SWF Metadata tag. -->\n\t<metadata>"
metaSectionFooter="\t</metadata>"
title="\t\t<title>${10}</title>"
description="\t\t<description>${11}</description>"
publisher="\t\t<publisher>${12}</publisher>"
creator="\t\t<creator>${13}</creator>"
lang="\t\t<language>${14}</language>"

# These are base value fields
rawMeta="\t<raw-metadata>${15}</raw-metadata>"
benchmark="\t<benchmark>${16}</benchmark>"
defaultColor="\t<default-background-color>${17}</default-background-color>"

# These values relate to developers using Remote Objects (either Flex Data Services or some form of AMF application server)
serviceString="${18}"
services="\t\t<services>$serviceString</services>"

# Here we do a quick CD to the location provided to this script as variable 1.
cd "$1"

# we use the -e switch with echo so that it converts the \n\t characters to newlines and tabs
createCompilerBody ()
{
	echo -e "$compilerSectionHeader"
	echo -e "$accessible"
	echo -e "$asWarnings"
	echo -e "$debug"
	echo -e "$keepAS"
	echo -e "$namespaceHeader"
	echo -e "$namespaceURI"
	echo -e "$manifestFile"
	echo -e "$namespaceFooter"
	echo -e "$optimized"
	if [ -n "$serviceString" ];
		then
			echo -e "$services"
	fi	
	echo -e "$compilerSectionFooter"
}

createMETABody ()
{
	echo -e "$metaSectionHeader"
	echo -e "$title"
	echo -e "$description"
	echo -e "$publisher"
	echo -e "$creator"
	echo -e "$lang"
	echo -e "$metaSectionFooter"
}

writeConfigFile ()
{
		echo "$xmlHeader"
		echo "$configHeader"
		echo -e "$benchmark"
		echo -e "$defaultColor"
		createCompilerBody 
		createMETABody
		echo -e "$rawMeta"
		echo "$configFooter"
		echo "$info"
}

writeConfigFile > "$2"

exit $?